The following tutorial will let you reproduce the plots that we are going to create at the workshop using R.
Please read carefully and follow the steps. Wherever you see the Code icon on the right you can click on it to see the actual code used in that section.
This tutorial will focus on analysing the updated data of the worldwide Novel Corona virus (COVID-19) pandemic.
There are several data sources available online. We will use the data collected from a range of official sources and hosted on the Our World in Data website (Mathieu et al. 2021).
To run R and RStudio on Binder, click on this badge - .
Start RStudio and create a new project named Workshop3 in a new folder (if you need a reminder ho to do it, check out Workshop1 Tutorial on BB).
Once RStudio restarts inside the project’s folder, create a new R script named Workshop3.R and 2 new folders, one named data for our input data and another named output for our plots.
For this analysis we will again use some packages from the Tidyverse, but this time we load the specific packages (which are supposed to be pre-installed on your computers) to try and avoid having to download the entire tidyverse. In addition to the Tidyverse packages we’ve got to know in the previous workshop we will use the plotly package to create interactive plots, paletteer for custom color palettes, readxl to read MS-Excel file, scales to format large numbers, lubridate to better handle dates, glue to paste together strings, patchwork to include several plots in a single figure and a few others to assist in getting the data into shape.
To install these packages, we will introduce a package called pacman that will assist in loading the required packages and installing them if they’re not already installed. To install it we use the install.packages('pacman') command, please note that the package name need to be quoted and that we only need to perform it once, or when we want or need to update the package. Once the package was installed, we can load its functions using the library(pacman) command and then load/install all the other packages at once with p_load() function.
# install required packages - needed only once! (comment with a # after first use)
install.packages('pacman')
# load required packages
library(pacman)
p_load(dplyr, tidyr, ggplot2, readr, paletteer, glue, scales, plotly, lubridate, patchwork, visdat)More information on installing and using R packages can be found in this tutorial.
Now that we’ve got RStudio up and running and our packages installed and loaded, we can read data into R from our local computer or from web locations using dedicated functions specific to the file type (.csv, .txt, .xlsx, etc.).
We will use the read_csv() command/function from the readr package (part of the tidyverse) to load the data directly from a file on the Our World in Data website into a variable of type data frame (table). If we don’t want to use external packages, we can use the read.csv() function from base R, which won’t automatically parse columns containing dates and in previous versions of R (< 4.0) will slightly change the structure of the resulting data frame (all text columns will be converted into factors).
> Note that in this case, we need to specify the column types because the data contains a lot of missing values that interfere with the automatic parsing of the column types._
Let’s use built-in functions for a brief data exploration, such as head() to show the first 10 rows of the data and str() for the type of data in each column (see detailed information on each variable in the data repository on GitHub):
## # A tibble: 6 x 67
## iso_code continent location date total_cases new_cases new_cases_smoot~
## <chr> <fct> <chr> <date> <dbl> <dbl> <dbl>
## 1 AFG Asia Afghanis~ 2020-02-24 5 5 NA
## 2 AFG Asia Afghanis~ 2020-02-25 5 0 NA
## 3 AFG Asia Afghanis~ 2020-02-26 5 0 NA
## 4 AFG Asia Afghanis~ 2020-02-27 5 0 NA
## 5 AFG Asia Afghanis~ 2020-02-28 5 0 NA
## 6 AFG Asia Afghanis~ 2020-02-29 5 0 NA
## # ... with 60 more variables: total_deaths <dbl>, new_deaths <dbl>,
## # new_deaths_smoothed <dbl>, total_cases_per_million <dbl>,
## # new_cases_per_million <dbl>, new_cases_smoothed_per_million <dbl>,
## # total_deaths_per_million <dbl>, new_deaths_per_million <dbl>,
## # new_deaths_smoothed_per_million <dbl>, reproduction_rate <dbl>,
## # icu_patients <dbl>, icu_patients_per_million <dbl>, hosp_patients <dbl>,
## # hosp_patients_per_million <dbl>, weekly_icu_admissions <dbl>, ...
## spec_tbl_df [167,936 x 67] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ iso_code : chr [1:167936] "AFG" "AFG" "AFG" "AFG" ...
## $ continent : Factor w/ 6 levels "Asia","Europe",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ location : chr [1:167936] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
## $ date : Date[1:167936], format: "2020-02-24" "2020-02-25" ...
## $ total_cases : num [1:167936] 5 5 5 5 5 5 5 5 5 5 ...
## $ new_cases : num [1:167936] 5 0 0 0 0 0 0 0 0 0 ...
## $ new_cases_smoothed : num [1:167936] NA NA NA NA NA NA 0.714 0 0 0 ...
## $ total_deaths : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_deaths : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_deaths_smoothed : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ total_cases_per_million : num [1:167936] 0.126 0.126 0.126 0.126 0.126 0.126 0.126 0.126 0.126 0.126 ...
## $ new_cases_per_million : num [1:167936] 0.126 0 0 0 0 0 0 0 0 0 ...
## $ new_cases_smoothed_per_million : num [1:167936] NA NA NA NA NA NA 0.018 0 0 0 ...
## $ total_deaths_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_deaths_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_deaths_smoothed_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ reproduction_rate : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ icu_patients : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ icu_patients_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ hosp_patients : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ hosp_patients_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ weekly_icu_admissions : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ weekly_icu_admissions_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ weekly_hosp_admissions : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ weekly_hosp_admissions_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ total_tests : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ total_tests_per_thousand : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests_per_thousand : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests_smoothed : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests_smoothed_per_thousand : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ positive_rate : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ tests_per_case : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ tests_units : chr [1:167936] NA NA NA NA ...
## $ total_vaccinations : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ people_vaccinated : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ people_fully_vaccinated : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ total_boosters : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_vaccinations : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_vaccinations_smoothed : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ total_vaccinations_per_hundred : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ people_vaccinated_per_hundred : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ people_fully_vaccinated_per_hundred : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ total_boosters_per_hundred : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_vaccinations_smoothed_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_people_vaccinated_smoothed : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ new_people_vaccinated_smoothed_per_hundred: num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ stringency_index : num [1:167936] 8.33 8.33 8.33 8.33 8.33 ...
## $ population : num [1:167936] 39835428 39835428 39835428 39835428 39835428 ...
## $ population_density : num [1:167936] 54.4 54.4 54.4 54.4 54.4 ...
## $ median_age : num [1:167936] 18.6 18.6 18.6 18.6 18.6 18.6 18.6 18.6 18.6 18.6 ...
## $ aged_65_older : num [1:167936] 2.58 2.58 2.58 2.58 2.58 ...
## $ aged_70_older : num [1:167936] 1.34 1.34 1.34 1.34 1.34 ...
## $ gdp_per_capita : num [1:167936] 1804 1804 1804 1804 1804 ...
## $ extreme_poverty : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ cardiovasc_death_rate : num [1:167936] 597 597 597 597 597 ...
## $ diabetes_prevalence : num [1:167936] 9.59 9.59 9.59 9.59 9.59 9.59 9.59 9.59 9.59 9.59 ...
## $ female_smokers : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ male_smokers : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ handwashing_facilities : num [1:167936] 37.7 37.7 37.7 37.7 37.7 ...
## $ hospital_beds_per_thousand : num [1:167936] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
## $ life_expectancy : num [1:167936] 64.8 64.8 64.8 64.8 64.8 ...
## $ human_development_index : num [1:167936] 0.511 0.511 0.511 0.511 0.511 0.511 0.511 0.511 0.511 0.511 ...
## $ excess_mortality_cumulative_absolute : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ excess_mortality_cumulative : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ excess_mortality : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## $ excess_mortality_cumulative_per_million : num [1:167936] NA NA NA NA NA NA NA NA NA NA ...
## - attr(*, "spec")=
## .. cols(
## .. iso_code = col_character(),
## .. continent = col_factor(levels = NULL, ordered = FALSE, include_na = FALSE),
## .. location = col_character(),
## .. date = col_date(format = ""),
## .. total_cases = col_double(),
## .. new_cases = col_double(),
## .. new_cases_smoothed = col_double(),
## .. total_deaths = col_double(),
## .. new_deaths = col_double(),
## .. new_deaths_smoothed = col_double(),
## .. total_cases_per_million = col_double(),
## .. new_cases_per_million = col_double(),
## .. new_cases_smoothed_per_million = col_double(),
## .. total_deaths_per_million = col_double(),
## .. new_deaths_per_million = col_double(),
## .. new_deaths_smoothed_per_million = col_double(),
## .. reproduction_rate = col_double(),
## .. icu_patients = col_double(),
## .. icu_patients_per_million = col_double(),
## .. hosp_patients = col_double(),
## .. hosp_patients_per_million = col_double(),
## .. weekly_icu_admissions = col_double(),
## .. weekly_icu_admissions_per_million = col_double(),
## .. weekly_hosp_admissions = col_double(),
## .. weekly_hosp_admissions_per_million = col_double(),
## .. total_tests = col_double(),
## .. new_tests = col_double(),
## .. total_tests_per_thousand = col_double(),
## .. new_tests_per_thousand = col_double(),
## .. new_tests_smoothed = col_double(),
## .. new_tests_smoothed_per_thousand = col_double(),
## .. positive_rate = col_double(),
## .. tests_per_case = col_double(),
## .. tests_units = col_character(),
## .. total_vaccinations = col_double(),
## .. people_vaccinated = col_double(),
## .. people_fully_vaccinated = col_double(),
## .. total_boosters = col_double(),
## .. new_vaccinations = col_double(),
## .. new_vaccinations_smoothed = col_double(),
## .. total_vaccinations_per_hundred = col_double(),
## .. people_vaccinated_per_hundred = col_double(),
## .. people_fully_vaccinated_per_hundred = col_double(),
## .. total_boosters_per_hundred = col_double(),
## .. new_vaccinations_smoothed_per_million = col_double(),
## .. new_people_vaccinated_smoothed = col_double(),
## .. new_people_vaccinated_smoothed_per_hundred = col_double(),
## .. stringency_index = col_double(),
## .. population = col_double(),
## .. population_density = col_double(),
## .. median_age = col_double(),
## .. aged_65_older = col_double(),
## .. aged_70_older = col_double(),
## .. gdp_per_capita = col_double(),
## .. extreme_poverty = col_double(),
## .. cardiovasc_death_rate = col_double(),
## .. diabetes_prevalence = col_double(),
## .. female_smokers = col_double(),
## .. male_smokers = col_double(),
## .. handwashing_facilities = col_double(),
## .. hospital_beds_per_thousand = col_double(),
## .. life_expectancy = col_double(),
## .. human_development_index = col_double(),
## .. excess_mortality_cumulative_absolute = col_double(),
## .. excess_mortality_cumulative = col_double(),
## .. excess_mortality = col_double(),
## .. excess_mortality_cumulative_per_million = col_double()
## .. )
We can also produce some descriptive statistics to better understand the data and the nature of each variable. The summary() function (as can be guessed by its name) provides a quick summary of basic descriptive statistics, such as the mean, min, max and quantiles for continuous numerical values.
## iso_code continent location
## Length:167936 Asia :36536 Length:167936
## Class :character Europe :37445 Class :character
## Mode :character Africa :39802 Mode :character
## North America:25169
## South America: 9608
## Oceania : 9329
## NA's :10047
## date total_cases new_cases new_cases_smoothed
## Min. :2020-01-01 Min. : 1 Min. : 0 Min. : 0
## 1st Qu.:2020-09-10 1st Qu.: 2050 1st Qu.: 1 1st Qu.: 7
## Median :2021-03-18 Median : 26658 Median : 79 Median : 108
## Mean :2021-03-12 Mean : 2593114 Mean : 11766 Mean : 11752
## 3rd Qu.:2021-09-13 3rd Qu.: 304329 3rd Qu.: 1071 3rd Qu.: 1159
## Max. :2022-03-12 Max. :456790241 Max. :4115804 Max. :3445357
## NA's :3047 NA's :3223 NA's :5212
## total_deaths new_deaths new_deaths_smoothed
## Min. : 1 Min. : 0.0 Min. : 0.000
## 1st Qu.: 80 1st Qu.: 0.0 1st Qu.: 0.143
## Median : 791 Median : 2.0 Median : 2.429
## Mean : 58259 Mean : 170.7 Mean : 172.317
## 3rd Qu.: 7369 3rd Qu.: 20.0 3rd Qu.: 21.286
## Max. :6040665 Max. :18021.0 Max. :14689.286
## NA's :20951 NA's :20930 NA's :23039
## total_cases_per_million new_cases_per_million new_cases_smoothed_per_million
## Min. : 0.0 Min. : 0.00 Min. : 0.000
## 1st Qu.: 634.4 1st Qu.: 0.04 1st Qu.: 1.632
## Median : 4845.7 Median : 11.49 Median : 18.982
## Mean : 30349.8 Mean : 170.25 Mean : 169.311
## 3rd Qu.: 38911.8 3rd Qu.: 101.97 3rd Qu.: 122.224
## Max. :706541.9 Max. :51427.49 Max. :16052.608
## NA's :3812 NA's :3988 NA's :5971
## total_deaths_per_million new_deaths_per_million
## Min. : 0.0 Min. : 0.000
## 1st Qu.: 18.9 1st Qu.: 0.000
## Median : 131.4 Median : 0.126
## Mean : 515.9 Mean : 1.684
## 3rd Qu.: 725.5 3rd Qu.: 1.369
## Max. :6339.5 Max. :453.772
## NA's :21703 NA's :21682
## new_deaths_smoothed_per_million reproduction_rate icu_patients
## Min. : 0.000 Min. :-0.07 Min. : 0.0
## 1st Qu.: 0.017 1st Qu.: 0.80 1st Qu.: 28.0
## Median : 0.292 Median : 0.99 Median : 146.0
## Mean : 1.686 Mean : 1.00 Mean : 899.6
## 3rd Qu.: 1.773 3rd Qu.: 1.18 3rd Qu.: 594.0
## Max. :144.167 Max. : 6.12 Max. :28891.0
## NA's :23785 NA's :40773 NA's :144227
## icu_patients_per_million hosp_patients hosp_patients_per_million
## Min. : 0.00 Min. : 0 Min. : 0.00
## 1st Qu.: 3.96 1st Qu.: 130 1st Qu.: 26.68
## Median : 13.39 Median : 709 Median : 85.67
## Mean : 23.91 Mean : 4188 Mean : 167.76
## 3rd Qu.: 34.72 3rd Qu.: 2769 3rd Qu.: 229.25
## Max. :177.28 Max. :154540 Max. :1544.08
## NA's :144227 NA's :143064 NA's :143064
## weekly_icu_admissions weekly_icu_admissions_per_million weekly_hosp_admissions
## Min. : 0.0 Min. : 0.00 Min. : 0
## 1st Qu.: 48.0 1st Qu.: 3.94 1st Qu.: 325
## Median : 220.0 Median : 11.06 Median : 1350
## Mean : 468.5 Mean : 15.41 Mean : 6009
## 3rd Qu.: 665.0 3rd Qu.: 20.34 3rd Qu.: 5204
## Max. :4838.0 Max. :221.21 Max. :154696
## NA's :162419 NA's :162419 NA's :156892
## weekly_hosp_admissions_per_million total_tests new_tests
## Min. : 0.00 Min. : 0 Min. : 1
## 1st Qu.: 23.98 1st Qu.: 381040 1st Qu.: 2481
## Median : 73.64 Median : 1970205 Median : 9804
## Mean :104.08 Mean : 17575921 Mean : 68302
## 3rd Qu.:142.60 3rd Qu.: 9258870 3rd Qu.: 38937
## Max. :781.38 Max. :827499466 Max. :3740296
## NA's :156892 NA's :97813 NA's :99900
## total_tests_per_thousand new_tests_per_thousand new_tests_smoothed
## Min. : 0.00 Min. : 0.00 Min. : 0
## 1st Qu.: 35.48 1st Qu.: 0.26 1st Qu.: 2103
## Median : 183.24 Median : 0.94 Median : 8546
## Mean : 754.25 Mean : 3.23 Mean : 60698
## 3rd Qu.: 719.71 3rd Qu.: 2.90 3rd Qu.: 35835
## Max. :29597.68 Max. :534.01 Max. :3080396
## NA's :97813 NA's :99900 NA's :82474
## new_tests_smoothed_per_thousand positive_rate tests_per_case
## Min. : 0.00 Min. :0.00 Min. : 1.0
## 1st Qu.: 0.23 1st Qu.:0.02 1st Qu.: 7.1
## Median : 0.90 Median :0.06 Median : 17.3
## Mean : 2.91 Mean :0.10 Mean : 197.7
## 3rd Qu.: 2.70 3rd Qu.:0.14 3rd Qu.: 52.6
## Max. :147.60 Max. :0.99 Max. :422065.6
## NA's :82474 NA's :87867 NA's :88464
## tests_units total_vaccinations people_vaccinated
## Length:167936 Min. :0.000e+00 Min. :0.000e+00
## Class :character 1st Qu.:6.079e+05 1st Qu.:3.942e+05
## Mode :character Median :4.860e+06 Median :2.976e+06
## Mean :1.768e+08 Mean :9.024e+07
## 3rd Qu.:3.042e+07 3rd Qu.:1.773e+07
## Max. :1.097e+10 Max. :5.000e+09
## NA's :122301 NA's :124535
## people_fully_vaccinated total_boosters new_vaccinations
## Min. :1.000e+00 Min. :1.000e+00 Min. : 0
## 1st Qu.:2.790e+05 1st Qu.:2.725e+03 1st Qu.: 6248
## Median :2.313e+06 Median :4.814e+05 Median : 41567
## Mean :7.152e+07 Mean :2.057e+07 Mean : 1174415
## 3rd Qu.:1.393e+07 3rd Qu.:4.256e+06 3rd Qu.: 278607
## Max. :4.460e+09 Max. :1.455e+09 Max. :54855684
## NA's :127260 NA's :149912 NA's :130218
## new_vaccinations_smoothed total_vaccinations_per_hundred
## Min. : 0 Min. : 0.00
## 1st Qu.: 1048 1st Qu.: 12.44
## Median : 9282 Median : 59.63
## Mean : 519990 Mean : 73.90
## 3rd Qu.: 66068 3rd Qu.:124.97
## Max. :43535350 Max. :338.97
## NA's :82018 NA's :122301
## people_vaccinated_per_hundred people_fully_vaccinated_per_hundred
## Min. : 0.00 Min. : 0.00
## 1st Qu.: 8.80 1st Qu.: 5.26
## Median : 36.77 Median : 27.68
## Mean : 38.19 Mean : 32.92
## 3rd Qu.: 64.80 3rd Qu.: 58.63
## Max. :124.69 Max. :121.59
## NA's :124535 NA's :127260
## total_boosters_per_hundred new_vaccinations_smoothed_per_million
## Min. : 0.00 Min. : 0
## 1st Qu.: 0.01 1st Qu.: 677
## Median : 3.39 Median : 2166
## Mean :12.88 Mean : 3289
## 3rd Qu.:21.58 3rd Qu.: 4713
## Max. :92.69 Max. :117497
## NA's :149912 NA's :82018
## new_people_vaccinated_smoothed new_people_vaccinated_smoothed_per_hundred
## Min. : 0 Min. : 0.00
## 1st Qu.: 422 1st Qu.: 0.02
## Median : 3944 Median : 0.07
## Mean : 213614 Mean : 0.15
## 3rd Qu.: 26838 3rd Qu.: 0.19
## Max. :21396353 Max. :11.75
## NA's :83360 NA's :83360
## stringency_index population population_density median_age
## Min. : 0.00 Min. :4.700e+01 Min. : 0.137 Min. :15.10
## 1st Qu.: 40.74 1st Qu.:1.172e+06 1st Qu.: 36.253 1st Qu.:22.20
## Median : 54.63 Median :8.478e+06 Median : 85.129 Median :29.90
## Mean : 54.48 Mean :1.473e+08 Mean : 464.163 Mean :30.57
## 3rd Qu.: 70.37 3rd Qu.:3.393e+07 3rd Qu.: 212.865 3rd Qu.:39.10
## Max. :100.00 Max. :7.875e+09 Max. :20546.766 Max. :48.20
## NA's :36712 NA's :1082 NA's :18580 NA's :28775
## aged_65_older aged_70_older gdp_per_capita extreme_poverty
## Min. : 1.144 Min. : 0.526 Min. : 661.2 Min. : 0.10
## 1st Qu.: 3.507 1st Qu.: 2.063 1st Qu.: 4449.9 1st Qu.: 0.60
## Median : 6.614 Median : 3.915 Median : 12951.8 Median : 2.20
## Mean : 8.761 Mean : 5.534 Mean : 19639.4 Mean :13.58
## 3rd Qu.:14.178 3rd Qu.: 8.678 3rd Qu.: 27936.9 3rd Qu.:21.20
## Max. :27.049 Max. :18.493 Max. :116935.6 Max. :77.60
## NA's :30283 NA's :29521 NA's :28095 NA's :75846
## cardiovasc_death_rate diabetes_prevalence female_smokers male_smokers
## Min. : 79.37 Min. : 0.990 Min. : 0.10 Min. : 7.70
## 1st Qu.:168.71 1st Qu.: 5.310 1st Qu.: 1.90 1st Qu.:21.60
## Median :243.81 Median : 7.170 Median : 6.30 Median :31.40
## Mean :260.24 Mean : 8.212 Mean :10.63 Mean :32.78
## 3rd Qu.:329.94 3rd Qu.:10.430 3rd Qu.:19.30 3rd Qu.:41.30
## Max. :724.42 Max. :30.530 Max. :44.00 Max. :78.10
## NA's :29835 NA's :22594 NA's :60864 NA's :62333
## handwashing_facilities hospital_beds_per_thousand life_expectancy
## Min. : 1.19 Min. : 0.10 Min. :53.28
## 1st Qu.: 19.35 1st Qu.: 1.30 1st Qu.:69.50
## Median : 49.84 Median : 2.40 Median :75.05
## Mean : 50.78 Mean : 3.03 Mean :73.58
## 3rd Qu.: 83.24 3rd Qu.: 4.00 3rd Qu.:78.93
## Max. :100.00 Max. :13.80 Max. :86.75
## NA's :98702 NA's :43082 NA's :11163
## human_development_index excess_mortality_cumulative_absolute
## Min. :0.394 Min. : -37726.1
## 1st Qu.:0.602 1st Qu.: -67.3
## Median :0.743 Median : 3482.2
## Mean :0.726 Mean : 38223.0
## 3rd Qu.:0.845 3rd Qu.: 25682.2
## Max. :0.957 Max. :1111864.5
## NA's :30360 NA's :162189
## excess_mortality_cumulative excess_mortality
## Min. :-28.45 Min. :-95.92
## 1st Qu.: -0.68 1st Qu.: -0.74
## Median : 6.13 Median : 7.23
## Mean : 9.48 Mean : 15.99
## 3rd Qu.: 14.60 3rd Qu.: 23.02
## Max. :111.01 Max. :375.00
## NA's :162189 NA's :162189
## excess_mortality_cumulative_per_million
## Min. :-1826.60
## 1st Qu.: -28.58
## Median : 478.56
## Mean : 985.44
## 3rd Qu.: 1667.35
## Max. : 9153.06
## NA's :162189
## # A tibble: 167,936 x 67
## iso_code continent location date total_cases new_cases new_cases_smoot~
## <chr> <fct> <chr> <date> <dbl> <dbl> <dbl>
## 1 OWID_WRL <NA> World 2022-03-12 456790241 1556688 1651453
## 2 OWID_WRL <NA> World 2022-03-11 455233553 1807277 1623829.
## 3 OWID_WRL <NA> World 2022-03-10 453426276 1817503 1608428.
## 4 OWID_WRL <NA> World 2022-03-09 451609116 1881823 1617683.
## 5 OWID_WRL <NA> World 2022-03-08 449727293 1844922 1585029.
## 6 OWID_WRL <NA> World 2022-03-07 447882371 1498717 1541542.
## 7 OWID_WRL <NA> World 2022-03-06 446383654 1153241 1528276.
## 8 OWID_WRL <NA> World 2022-03-05 445230413 1363321 1515639.
## 9 OWID_WRL <NA> World 2022-03-04 443867092 1699470 1510840
## 10 OWID_WRL <NA> World 2022-03-03 442167635 1882290 1495768.
## # ... with 167,926 more rows, and 60 more variables: total_deaths <dbl>,
## # new_deaths <dbl>, new_deaths_smoothed <dbl>, total_cases_per_million <dbl>,
## # new_cases_per_million <dbl>, new_cases_smoothed_per_million <dbl>,
## # total_deaths_per_million <dbl>, new_deaths_per_million <dbl>,
## # new_deaths_smoothed_per_million <dbl>, reproduction_rate <dbl>,
## # icu_patients <dbl>, icu_patients_per_million <dbl>, hosp_patients <dbl>,
## # hosp_patients_per_million <dbl>, weekly_icu_admissions <dbl>, ...
What are the metadata columns that describe our observations?
continent
location
date
Why do we have observations with the continent as NA?
# check which location have continent as NA
covid_data %>% filter(is.na(continent)) %>% count(location)## # A tibble: 13 x 2
## location n
## <chr> <int>
## 1 Africa 759
## 2 Asia 781
## 3 Europe 780
## 4 European Union 780
## 5 High income 781
## 6 International 765
## 7 Low income 749
## 8 Lower middle income 781
## 9 North America 781
## 10 Oceania 778
## 11 South America 750
## 12 Upper middle income 781
## 13 World 781
Some rows contain summarised data of entire continents/World, we'll need to remove those
We can see that most of our data contains ‘0’ (check the difference between the median and the mean in total_cases and total_deaths columns). Just to confirm that, let’s plot a histogram of all the confirmed cases
ggplot(covid_data, aes(x=total_cases)) +
geom_histogram(fill="lightskyblue") +
theme_bw(def_text_size)The data is evolving over days (a time-series), to there’s no point treating it as a random population sample.
Let’s look at confirmed cases and total deaths data for the 10 most affected countries (to date). To find out these countries so we need to wrangle our data a little bit using the following steps:
filter(!is.na(continent)group_by()arrange(desc(date))slice(1) and remove grouping with ungroup()inner_join()Optional step:
location variable as a factor and order it so the countries will be ordered in the legend by the number of cases withThen we can look at the data as a table and make a plot with the number of cases in the y-axis and date in the x-axis.
# find the 10 most affected countries (to date)
latest_data <- covid_data %>% filter(!is.na(continent)) %>%
group_by(location) %>% arrange(desc(date)) %>% slice(1) %>% ungroup()
most_affected_countries <- latest_data %>%
arrange(desc(total_deaths)) %>% slice(1:10) %>%
select(location)
# subset just the data from the 10 most affected countries and order them from the most affected to the least one
most_affected_data <- covid_data %>%
inner_join(most_affected_countries) %>%
mutate(Country=factor(location, levels = most_affected_countries$location))
# create a line plot the data of total cases
ggplot(most_affected_data, aes(x=date, y=total_cases, colour=Country)) +
geom_line(size=0.75) + scale_y_continuous(labels=comma) +
scale_color_paletteer_d("rcartocolor::Bold") +
labs(color="Country", y = "Total COVID-19 cases") +
theme_bw(def_text_size)It’s a bit hard to figure out how the pandemic evolved because the numbers in US, Brazil and India are an order of magnitude larger than the rest (which are very close to each other). How can we make it more visible (and also improve how of the dates appear in the x-axis)?
# better formatting of date axis, log scale
plot <- ggplot(most_affected_data, aes(x=date, y=total_cases, colour=Country)) +
geom_line(size=0.75) + scale_y_log10(labels=comma) +
scale_x_date(NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
scale_color_paletteer_d("rcartocolor::Bold") +
labs(color="Country", y = "Total COVID-19 cases") +
theme_bw(def_text_size)
# show an interactive plot
ggplotly(plot)Why did we get a warning message and why the graphs don’t start at the bottom of the x-axis? How can we solve it? What can we infer from the graph (exponential increase)?
What happens when we take the log of 0?? Can we remove those 0s with the `filter()` function (or add a very small number to them)?
We can see a very similar trend for most countries and while the curve has flattened substantially in April last year, the numbers are still rising. It is also evident that Europe got hit by a second wave arount October last year and India in April this year.
Let’s have a look at the total deaths in these countries (and get rid of the minor grid lines to make Frank happy)
# create a line plot the data of total deaths
ggplot(most_affected_data, aes(x=date, y=total_deaths, colour=Country)) +
geom_line(size=0.75) + scale_y_continuous(labels=comma) +
scale_x_date(NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
scale_color_paletteer_d("rcartocolor::Bold") +
labs(color="Country", y = "Total deaths") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank()) # remove minor grid linesLet’s have a look at the number of vaccinated people.
# vaccination rates
ggplot(most_affected_data, aes(x=date, y=people_vaccinated, colour=Country)) +
geom_line(size=0.75) + scale_y_continuous(labels=comma) +
scale_color_paletteer_d("rcartocolor::Bold") +
scale_x_date(NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
labs(color="Country", y="People Vaccinated") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank())The graphs are “broken”, meaning that it is not continuous and we have some missing data.
Let’s visualise some of the variables in our data and assess “missingness”.
# visualise missingness
vis_dat(covid_data %>% filter(date>dmy("01-01-2021")) %>%
select(continent, location, total_cases, total_deaths,
hosp_patients, people_vaccinated, people_fully_vaccinated))# find which countries has the most number of observations (least missing data)
covid_data %>% filter(!is.na(continent), !is.na(people_vaccinated)) %>% # group_by(location) %>%
count(location) %>% arrange(desc(n)) %>% print(n=30)## # A tibble: 219 x 2
## location n
## <chr> <int>
## 1 Norway 464
## 2 United States 454
## 3 Latvia 453
## 4 Denmark 451
## 5 Canada 449
## 6 Israel 449
## 7 Liechtenstein 445
## 8 Switzerland 445
## 9 Czechia 441
## 10 Italy 441
## 11 Lithuania 441
## 12 Chile 440
## 13 Estonia 440
## 14 Germany 440
## 15 Slovenia 440
## 16 France 439
## 17 Belgium 437
## 18 Ireland 436
## 19 Singapore 433
## 20 United Kingdom 425
## 21 Greece 421
## 22 India 411
## 23 Bahrain 409
## 24 Brazil 409
## 25 Malta 407
## 26 Indonesia 406
## 27 Ecuador 402
## 28 Peru 394
## 29 Luxembourg 392
## 30 Turkey 391
## # ... with 189 more rows
covid_data %>% filter(!is.na(continent), !is.na(hosp_patients)) %>% # group_by(location) %>%
count(location) %>% arrange(desc(n)) %>% print(n=30)## # A tibble: 38 x 2
## location n
## <chr> <int>
## 1 Italy 748
## 2 Estonia 744
## 3 Sweden 744
## 4 Netherlands 743
## 5 Israel 741
## 6 Portugal 737
## 7 Canada 734
## 8 Czechia 732
## 9 Hungary 732
## 10 Slovakia 731
## 11 Cyprus 728
## 12 Belgium 727
## 13 Ireland 727
## 14 Slovenia 727
## 15 France 725
## 16 Luxembourg 721
## 17 Malaysia 719
## 18 United Kingdom 714
## 19 Australia 712
## 20 Austria 711
## 21 Serbia 711
## 22 Switzerland 711
## 23 Denmark 709
## 24 Latvia 706
## 25 Bulgaria 700
## 26 Singapore 692
## 27 Croatia 691
## 28 Poland 681
## 29 Malta 677
## 30 Norway 630
## # ... with 8 more rows
Now we can focus on a subset of countries that have more complete vaccination and hospitalisation rates data, so we could compare them.
countries_subset <- c("Italy", "United States", "Israel", "United Kingdom", "France", "Czechia")
# subset our original data to these countries
hosp_data <- covid_data %>% filter(location %in% countries_subset)
# define a new colour palette for these countries
col_pal <- "ggsci::category10_d3"Let’s look at hospitalisation rates first.
ggplot(hosp_data, aes(x=date, y = hosp_patients,colour=location)) +
geom_line(size=0.75) + scale_y_continuous(labels=comma) +
scale_color_paletteer_d(col_pal) +
scale_x_date(name = NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
labs(color="Country", y = "Hospitalised patients") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank())Can you identify the “waves” in each country?
It’s hard to see the details in the countries with lower number of hospitalised patients, how can we improve the visualisation?
Look at hospitalision rates proportional to the population size!
# hosp per population size
p1 <- ggplot(hosp_data,
aes(x=date, y = hosp_patients_per_million,colour=location)) +
geom_line(size=0.75) +
scale_y_continuous(labels=comma) +
scale_color_paletteer_d(col_pal) +
scale_x_date(name = NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
labs(color="Country", y = "Hospitalised patients (per million)") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank())
p1Now let’s try to compare this to vaccination rates
# total vaccination per population
p2 <- ggplot(hosp_data,
aes(x=date, y = people_fully_vaccinated_per_hundred/100 ,colour=location)) +
geom_line(size=0.75, linetype="dashed") +
guides(color = guide_legend(override.aes = list(linetype="solid") ) ) +
scale_y_continuous(labels=percent) +
scale_color_paletteer_d(col_pal) +
scale_x_date(name = NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
labs(color="Country", y = "Fully vaccinated (percent)") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank())
p2What will be the best way to compare these values?
p1_narrow <- p1 + guides(color=FALSE) +
scale_x_date(name = NULL,
breaks = breaks_width("4 months"),
labels = label_date_short())
p2_narrow <- p2 +
scale_x_date(name = NULL,
breaks = breaks_width("4 months"),
labels = label_date_short())
(p1_narrow + guides(color=FALSE))+ p2_narrow + plot_layout(guides = 'collect')# show graphs side by sideMaybe like this:
(p1 + guides(color=FALSE)) / (p2 + theme(legend.position = "bottom")) #+ plot_layout(guides = 'collect')# show graphs on top of each otherAny suggestions?
There's a lot of empty "real estate" in the vaccination graph, maybe we could trim off 2020?
p3 <- ggplot(hosp_data,
aes(x=date, y = people_fully_vaccinated_per_hundred/100 ,colour=location)) +
geom_line(size=0.75, linetype="dashed") +
guides(color = guide_legend(override.aes = list(linetype="solid") ) ) +
scale_y_continuous(labels=percent) +
scale_color_paletteer_d(col_pal) +
scale_x_date(name = NULL,
limits = c(dmy("01-01-2021"), NA),
breaks = breaks_width("6 months"),
labels = label_date_short()) +
labs(color="Country", y = "Fully vaccinated (percent)") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank())
(p1_narrow + guides(color=FALSE)) + p3 + plot_layout(guides = 'collect', widths = c(2, 1)) # maybe like this?Let’s try to present them on the same graph (note the trick with the secondary y-axis).
# show on the same graph
p4 <- ggplot(hosp_data,
aes(x=date, colour=location)) +
geom_line(aes(y = hosp_patients_per_million), size=0.75) +
geom_line(aes(y = people_fully_vaccinated_per_hundred*10), size=0.75, linetype="dashed") +
scale_y_continuous(labels=comma, name = "Hospitalised patients per million (solid)",
# Add a second axis and specify its features
sec.axis = sec_axis(trans=~./10, name="Fully vaccinated per hundred (dashed)")) +
scale_color_paletteer_d(col_pal) +
scale_x_date(NULL,
breaks = breaks_width("2 months"),
labels = label_date_short()) +
labs(color="Country") +
theme_bw(def_text_size) +
theme(panel.grid.minor = element_blank())
p4Probably best to present them on the same graph (note the trick with the secondary y-axis), but for each country separately (done with the facet_wrap() function).
# show on the same graph, but separate each country
p4 +
scale_x_date(NULL,
breaks = breaks_width("4 months"),
labels = label_date_short()) +
facet_wrap(~location)Save the plot to a folder.
# create output folder
dir.create("./output", showWarnings = FALSE)
# save the plot to pdf file
ggsave("output/hospit_vacc_rates_facet_country.pdf", width=14, height = 8)1. Mortalities (Case Fatality Rate)?
2. Suggestions? (cases per population density, vaccination rates by country income, deaths by number of beds per capita, etc.
3. Level of reporting in each country...
Best way to conclude the presentation about our new Bachelor program in Environmental Data Science in @CZUvPraze. Small edit in the programming language used in the original strip of @xkcdComic. Sorry Pearl users :) #DataScience #rstats @FesCuls pic.twitter.com/KBkjLgFNwR
— Yannis Markonis (@YannisMarkonis) June 25, 2020
Please contact me at i.bar@griffith.edu.au for any questions or comments.
Mathieu E, Ritchie H, Ortiz-Ospina E, et al. (2021) A global database of COVID-19 vaccinations. Nat Hum Behav 5:947–953. doi: 10.1038/s41562-021-01122-8